package com.onlylemi.mapview.library.layer;


import com.onlylemi.mapview.library.MapView;
import com.onlylemi.mapview.library.ResourceTable;
import com.onlylemi.mapview.library.utils.MapMath;
import com.onlylemi.mapview.library.utils.MapUtils;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.Point;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.TouchEvent;

import java.util.List;

/**
 * MarkLayer
 *
 * @author: onlylemi
 */
public class MarkLayer extends MapBaseLayer {

    private List<Point> marks;
    private List<String> marksName;
    private MarkIsClickListener listener;

    private PixelMap bmpMark;
    private PixelMap bmpMarkTouch;

    private float radiusMark;
    private boolean isClickMark = false;
    private int num = -1;

    private Paint paint;

    public MarkLayer(MapView mapView) {
        this(mapView, null, null);
    }

    public MarkLayer(MapView mapView, List<Point> marks, List<String> marksName) {
        super(mapView);
        this.marks = marks;
        this.marksName = marksName;

        initLayer();
    }

    private void initLayer() {
        radiusMark = setValue(10f);

        bmpMark = MapUtils.getPixelMap(mapView.getContext(), ResourceTable.Media_mark);
        bmpMarkTouch = MapUtils.getPixelMap(mapView.getContext(), ResourceTable.Media_mark_touch, 92, 124);

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
    }

    @Override
    public void onTouch(TouchEvent event) {
        if (marks != null) {
            if (!marks.isEmpty()) {
                float[] goal = mapView.convertMapXYToScreenXY(event.getPointerPosition(0).getX(),
                        event.getPointerPosition(0).getY());
                for (int i = 0; i < marks.size(); i++) {
                    if (MapMath.getDistanceBetweenTwoPoints(goal[0], goal[1],
                            marks.get(i).getPointX() - (float) bmpMark.getImageInfo().size.width / 2,
                            marks.get(i).getPointY() - (float) bmpMark.getImageInfo().size.height / 2) <= 50) {
                        num = i;
                        isClickMark = true;
                        break;
                    }

                    if (i == marks.size() - 1) {
                        isClickMark = false;
                    }
                }
            }

            if (listener != null && isClickMark) {
                listener.markIsClick(num);
                mapView.refresh();
            }
        }
    }

    @Override
    public void draw(Canvas canvas, Matrix currentMatrix, float currentZoom, float
            currentRotateDegrees) {
        if (isVisible && marks != null) {
            canvas.save();
            if (!marks.isEmpty()) {
                for (int i = 0; i < marks.size(); i++) {
                    Point mark = marks.get(i);
                    float[] goal = {mark.getPointX(), mark.getPointY()};
                    currentMatrix.mapPoints(goal);

                    paint.setColor(Color.BLACK);
                    paint.setTextSize((int) radiusMark);

                    //mark name
                    if (mapView.getCurrentZoom() > 1.0 && marksName != null
                            && marksName.size() == marks.size()) {
                        canvas.drawText(paint, marksName.get(i), goal[0] - radiusMark, goal[1]
                                - radiusMark / 2);
                    }

                    //mark ico
                    canvas.drawPixelMapHolder(new PixelMapHolder(bmpMark),
                            goal[0] - (float) bmpMark.getImageInfo().size.width / 2,
                            goal[1] - (float) bmpMark.getImageInfo().size.height / 2, paint);
                    if (i == num && isClickMark) {
                        canvas.drawPixelMapHolder(new PixelMapHolder(bmpMarkTouch),
                                goal[0] - (float) bmpMarkTouch.getImageInfo().size.width / 2,
                                goal[1] - bmpMarkTouch.getImageInfo().size.height, paint);
                    }
                }
            }
            canvas.restore();
        }
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public List<Point> getMarks() {
        return marks;
    }

    public void setMarks(List<Point> marks) {
        this.marks = marks;
    }

    public List<String> getMarksName() {
        return marksName;
    }

    public void setMarksName(List<String> marksName) {
        this.marksName = marksName;
    }

    public boolean isClickMark() {
        return isClickMark;
    }

    /**
     * setMarkIsClickListener
     *
     * @param listener
     */
    public void setMarkIsClickListener(MarkIsClickListener listener) {
        this.listener = listener;
    }

    public interface MarkIsClickListener {
        void markIsClick(int num);
    }
}
